<template>
  <div class="cart">
    <h2>购物车</h2>
    <div v-if="cartItems.length === 0">购物车为空</div>
    <ul v-else>
      <li v-for="item in cartItems" :key="item.id">
        {{ item.name }} - ￥{{ item.price }} × {{ item.quantity }}
        <button @click="removeItem(item.id)">-</button>
      </li>
    </ul>
    <div>总价: ￥{{ totalPrice }}</div>
    <div>商品数量: {{ cartItemCount }}</div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions } from 'vuex'

export default {
  name: 'Cart',
  computed: {
    ...mapState(['cartItems']),
    ...mapGetters(['totalPrice', 'cartItemCount'])
  },
  methods: {
    ...mapActions(['removeFromCart']),
    removeItem(productId) {
      this.removeFromCart(productId)
    }
  }
}
</script>

<style scoped>
.cart {
  border: 1px solid #ccc;
  padding: 20px;
  margin: 20px;
}
ul {
  list-style: none;
  padding: 0;
}
li {
  margin: 10px 0;
}
button {
  margin-left: 10px;
}
</style>